local kl__jimu = fk.CreateSkill{
  name = "kl__jimu",
  tags = { Skill.Rhyme, Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["kl__jimu"] = "急募",
  [":kl__jimu"] = "转换技，锁定技，"..
  "<br>阳：你的【杀】视为【闪】，当你每回合首次失去【闪】后，将这张牌置于你的武将牌上，回合结束时你获得之并摸一张牌；"..
  "<br>阴：你的【闪】视为【杀】，你对有护甲或未受伤的角色使用牌无距离限制，且造成伤害时，此伤害+1。"..
  "<br>转换：当你发动【射书】后。",

  ["$kl__jimu1"] = "将无威严，何以统率三军。",
}

kl__jimu:addEffect("filter", {
  card_filter = function(self, card, player, isJudgeEvent)
    if player:hasSkill(kl__jimu.name) then
      if player:getSwitchSkillState(kl__jimu.name, false) == fk.SwitchYang then
        return card.trueName == "slash"
      else
        return card.trueName == "jink"
      end
    end
  end,
  view_as = function(self, player, card)
    if player:getSwitchSkillState(kl__jimu.name, false) == fk.SwitchYang then
      return Fk:cloneCard("jink", card.suit, card.number)
    else
      return Fk:cloneCard("slash", card.suit, card.number)
    end
  end,
})

kl__jimu:addEffect(fk.BeforeCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(kl__jimu.name, true) then
      local first = player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == player then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                if Fk:getCardById(info.cardId).trueName == "jink" then
                  return true
                end
              end
            end
          end
        end
      end, Player.HistoryTurn)[1]
      if first and first.data == data then
        return true
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            if Fk:getCardById(info.cardId).trueName == "jink" then
              move.extra_data = move.extra_data or {}
              move.extra_data.kl_jimu = info.cardId
              break
            end
          end
        end
      end
    end
  end,
})

kl__jimu:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(kl__jimu.name) and player:getSwitchSkillState(kl__jimu.name, false) == fk.SwitchYang then
      local card = {}
      local first = player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
        for _, move in ipairs(e.data) do
          if move.from == player then
            if move.extra_data and move.extra_data.kl_jimu then
              table.insert(card, move.extra_data.kl_jimu)
              return true
            end
          end
        end
      end, Player.HistoryTurn)[1]
      if first and first.data == data then
        event:setCostData(self, { card = card })
        return true
      end
    end
  end,
  on_use = function (self, event, target, player)
    local card = event:getCostData(self).card
    player:addToPile(kl__jimu.name, card, true, kl__jimu.name, player, player.room:getAllPlayers())
  end,
})

kl__jimu:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target and player:hasSkill(kl__jimu.name) and #player:getPile(kl__jimu.name) > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = #player:getPile(kl__jimu.name)
    room:obtainCard(player, player:getPile(kl__jimu.name), true, fk.ReasonPrey, player, kl__jimu.name)
    room:drawCards(player, num, kl__jimu.name)
  end,
})

kl__jimu:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    if player:hasSkill(kl__jimu.name) and player:getSwitchSkillState(kl__jimu.name, false) ~= fk.SwitchYang then
      return to and (to.shield > 0 or not to:isWounded())
    end
  end,
})

kl__jimu:addEffect(fk.DamageCaused, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(kl__jimu.name) and player:getSwitchSkillState(kl__jimu.name, false) ~= fk.SwitchYang then
      return data.to.shield > 0 or not data.to:isWounded()
    end
  end,
  on_use = function(self, event, target, player, data)
    data:changeDamage(1)
  end,
})


return kl__jimu
